package com.wujige.index;

import com.wujige.utils.ClinetUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


/**
 * @author: lipan
 * @date: 2021/4/26 20:48
 */

public class IndexOpertionTest {


    Logger logger= LogManager.getLogger(IndexOpertionTest.class);

    RestHighLevelClient client=null;

    @Before
    public void OpenConection(){

        client=ClinetUtils.getClient();
    }

    /**
     * 创建一个 名为 user2的索引
     * @throws Exception
     */
    @Test
    public void testCreatIndex() throws Exception{

        CreateIndexRequest createIndexRequest=new CreateIndexRequest("user2");
        CreateIndexResponse response = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        if(response.isAcknowledged()){
            logger.info("创建索引成功");
        }
    }


    @Test
    public void testSearchIndex() throws Exception{
        GetIndexRequest getIndexRequest=new GetIndexRequest("user2");
        GetIndexResponse response = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);

        logger.info(response.getAliases());
        logger.info(response.getMappings());
        logger.info(response.getSettings());



    }

    @Test
    public void testDeleteIndex() throws Exception{
        AcknowledgedResponse delete = client.indices().delete(new DeleteIndexRequest("user2"), RequestOptions.DEFAULT);

        if (delete.isAcknowledged()){
            logger.error("删除成功");
        }
    }


    @After
    public void closeConnection(){
        ClinetUtils.close();
    }





}
